<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        border: 1px solid red;
      }
    </style>
  </head>
  <body>
    <div class="box">box</div>
    <div class="box box1">box1</div>
    <img id="chrome" src="./compatible_chrome.gif" alt="" draggable="true" />
    <img id="fox" src="./compatible_firefox.gif" alt="" draggable="true" />

    <script>
      let img = document.querySelector("img");
      let fox = document.querySelector("#fox");
      let box = document.querySelector(".box");
      let box1 = document.querySelector(".box1");

      //   拖动ing
      img.ondrag = function (event) {
        // console.log("拖动", event);
      };

      //   拖动开始
      img.ondragstart = function (event) {
        console.log("拖动开始");

        // 设置拖动事件传递的值
        // event.dataTransfer.setData("Text", event.target.id);
        // event.dataTransfer.dropEffect = "none";
        event.dataTransfer.setData("text/plain", event.target.id);
      };

      //   拖动结束
      img.ondragend = function (event) {
        console.log("拖动结束");
      };

      fox.ondragstart = function (event) {
        event.dataTransfer.setData("text/plain", event.target.id);
      };

      //   拖动经过over元素
      box.ondragover = function (event) {
        //   阻止浏览器默认行为（禁止放）
        event.preventDefault();
        console.log("拖动经过了box");
      };
      box.ondrop = function (event) {
        // 获取一开始拖动的时候设置的值
        event.preventDefault();
        console.log("放在box内部", event.dataTransfer.getData("text/plain"));
        let id = event.dataTransfer.getData("text/plain");
        box.appendChild(document.querySelector("#" + id));
      };

      box1.ondragover = function (event) {
        event.preventDefault();
      };
      box1.ondrop = function (event) {
        // 获取一开始拖动的时候设置的值
        event.preventDefault();
        let id = event.dataTransfer.getData("text/plain");
        box1.appendChild(document.querySelector("#" + id));
      };
    </script>



    <!-- https://www.17sucai.com/pins/demo-show?id=23233 -->
  </body>
</html>
